package com.fingard.dsp.bank.directbank.boqh01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.directbank.chanpay01.chanpay01Util.S;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;

/**
 * @ClassName BOQH01Balance
 * @Author Zhengzy
 * @Date 2020/3/12 13:55
 * @Version 1.0
 **/
public class BOQH01Balance extends BOQH01Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        // 创建返回给ats的对象
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();
        try{
            while (p_req7506.readRecord()) {
                RetAtsBalDetail detail = new RetAtsBalDetail();
                String cstNo = getBankConfig().getValueAsString("CSTNO");
                // 组装报文
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                XElement bankData = new XElement("BankData", "");
                XElement opReq = new XElement("opReq", "");
                opReq.addChild(new XElement("cstNo", cstNo));// 企业编号
                Date date = new Date();
                opReq.addChild(new XElement("serialNo", "000" + Format.DateTime17SSSFormat.format(date)));// 企业流水号固定为20位，不允许重复
                opReq.addChild(new XElement("reqTime", Format.DateTime14Format.format(date)));// 客户端请求时间yyyyMMddHHmmss
                XElement ReqParam =  new XElement("ReqParam", "");
                ReqParam.addChild(new XElement("accountNo", p_req7506.readedDetail.actNum));
                opReq.addChild(ReqParam);
                bankData.addChild(opReq);
                xmlTextWriter.writeXElement(bankData);
                xmlTextWriter.writeEndDocument();

                //发送给银行
                String[] bankResult = sendToBank(xmlTextWriter.toXmlString(), "CBE001");
                /*// 测试 返回报文
                String[] bankResult = {"", "<BankData>\n" +
                        "   <opReq>\n" +
                        "      <opResult>\n" +
                        "          <CurrentBalance>16500</CurrentBalance>\n" +
                        "          <FreeBalance>0</FreeBalance>\n" +
                        "      <opResult>\n" +
                        "   </opReq>\n" +
                        "</BankData>"};*/

                if (bankResult[0].length() == 0) {
                    // 解析签名
                    String[] invildArr = IsResultInvlid(bankResult[1]);
                    if ("true".equals(invildArr[2])){
                        StringBuilder tmpSbMsg = new StringBuilder();
                        XmlTextReader tmpBkReader = loadBankRespXmlReader(invildArr[3], tmpSbMsg);
                        if (tmpBkReader != null) {// 有返回内容
                            detail.respCode = "0";
                            detail.actNum = p_req7506.readedDetail.actNum;
                            detail.actArea = p_req7506.readedDetail.actArea;
                            detail.currBal = tmpBkReader.getSimpleText("CurrentBalance");
                            detail.frozenBal = tmpBkReader.getSimpleText("FreeBalance");
                        }
                    } else {
                        retAts.writeRespCode(invildArr[0]);// 分别对应{RespCode，RespInfo，boolean, 报文}
                        retAts.writeRespInfo(invildArr[1]);
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(bankResult[1]);
                }
                retAts.writeRetDetail(detail, p_req7506.isBatch);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            WriteBankLogLn(e.getMessage());
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
